home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / local / sbin / boot-profile next >
Encoding:
Text File  |  2013-01-09  |  2.7 KB  |  88 lines

  1. #!/usr/bin/env python
  2.  
  3. from pyinotify import WatchManager, Notifier, \
  4.     ThreadedNotifier, ProcessEvent, IN_OPEN, IN_ACCESS, IN_CREATE, IN_MOVED_TO
  5. import re
  6. import sys
  7. import atexit
  8. from signal import signal, SIGTERM
  9. import os.path
  10.  
  11. # Ignore files matching this regular expression
  12. IGNORE_RE = "^/(tmp|sys|proc|dev|live/cow)"
  13.  
  14. # Remove the following prefix (except the last /) from all paths
  15. IGNORE_PREFIX="/live/rofs/filesystem.squashfs/"
  16.  
  17. class ProfileProcessor(ProcessEvent):
  18.     def __init__(self, profile_path):
  19.         self.priority = 32767
  20.         self.files = {}
  21.         self.ignored_files = {}
  22.         self.ignore_re = re.compile(IGNORE_RE)
  23.         self.profile_path = profile_path
  24.  
  25.     def add_file(self, path):
  26.         if ' ' in path:
  27.             # Skip path with white spaces: mksquashfs -sort does not
  28.             # handle them!  fscanf(fd, "%s %d", ...)
  29.             return
  30.         if path.startswith(IGNORE_PREFIX):
  31.             path = path[len(IGNORE_PREFIX)-1:]
  32.         if not self.files.has_key(path):
  33.             self.files[path] = self.priority
  34.             self.priority -= 1
  35.  
  36.     def ignore_file(self, path):
  37.         if path.startswith(IGNORE_PREFIX):
  38.             path = path[len(IGNORE_PREFIX)-1:]
  39.         self.ignored_files[path] = None
  40.  
  41.     def process_IN_OPEN(self, event):
  42.         if not event.dir:
  43.             self.add_file(event.pathname)
  44.  
  45.     def process_IN_ACCESS(self, event):
  46.         self.add_file(event.pathname)
  47.  
  48.     def process_IN_CREATE(self, event):
  49.         self.ignore_file(event.pathname)
  50.  
  51.     def process_IN_MOVED_TO(self, event):
  52.         self.ignore_file(event.pathname)
  53.  
  54.     def is_excluded(self, path):
  55.         if path.startswith(IGNORE_PREFIX):
  56.             path = path[len(IGNORE_PREFIX)-1:]
  57.         return self.ignore_re.match(path)
  58.  
  59.     def end_profiling(self):
  60.         profile = open(self.profile_path, 'w')
  61.         priorities = {}
  62.         for path, priority in self.files.iteritems():
  63.             if not self.ignored_files.has_key(path):
  64.                 priorities[priority] = path
  65.         keys = priorities.keys()
  66.         keys.sort(reverse=True)
  67.         for key in keys:
  68.             profile.write("%-68s %s\n" % (priorities[key][1:], key))
  69.         profile.close()
  70.  
  71. def main():
  72.     if len(sys.argv) < 2:
  73.         print >>sys.stderr, "usage: %s <new-profile>" % sys.argv[0]
  74.         sys.exit(0)
  75.  
  76.     wm = WatchManager()
  77.     profiler = ProfileProcessor(sys.argv[1])
  78.  
  79.     atexit.register(profiler.end_profiling)
  80.     signal(SIGTERM, lambda signum, stack_frame: sys.exit(0))
  81.  
  82.     notifier = Notifier(wm, profiler)
  83.     wm.add_watch('/', IN_OPEN | IN_ACCESS | IN_CREATE | IN_MOVED_TO, rec=True, exclude_filter=profiler.is_excluded)
  84.     notifier.loop(daemonize=True, pid_file='/boot-profile.pid')
  85.  
  86. if __name__ == '__main__':
  87.     main()
  88.